package com.zhangxi.websqlit.contact.control;

import com.zhangxi.websqlit.common.control.BaseController;
import com.zhangxi.websqlit.contact.entity.AddFriendsEntity;
import com.zhangxi.websqlit.contact.entity.SearchChatEntity;
import com.zhangxi.websqlit.contact.service.ContactService;
import com.zhangxi.websqlit.mybatis.entity.ChatReportEntity;
import com.zhangxi.websqlit.mybatis.entity.PtContactEnity;
import com.zhangxi.websqlit.user.entity.UserSearchEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/contact")
public class ContactController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private ContactService contactService;

    /**
     * 根据用户ID查询通迅录
     * @param userSearchEntity
     * @return
     */
    @RequestMapping("/queryContactListById")
    @ResponseBody
    public String queryContactListById(UserSearchEntity userSearchEntity){
        logger.info(userSearchEntity);
        List<PtContactEnity> list = contactService.queryContactListById(userSearchEntity);
        return jsonDataSuccess("user.query.contact.list",list);
    }

    /**
     * 根据用户ID查询消息列表
     * @param userSearchEntity
     * @return
     */
    @RequestMapping("/queryMessageListById")
    @ResponseBody
    public String queryMessageListById(UserSearchEntity userSearchEntity){
        logger.info(userSearchEntity);
        List<PtContactEnity> list = contactService.queryMessageListById(userSearchEntity);
        return jsonDataSuccess("user.query.message.list",list);
    }

    /**
     * 根据用户ID和好朋ID,更新好朋友备注
     * @param contactEnity
     * @return
     */
    @RequestMapping("/updateRemarkById")
    @ResponseBody
    public String updateRemarkById(PtContactEnity contactEnity){
        logger.info(contactEnity);
        contactService.updateRemarkById(contactEnity);
        return jsonSuccess("contact.update.remark.success");
    }
    /**
     * 通过用户ID\好朋友ID\备注增加好朋
     */
    @RequestMapping("/addFriendsById")
    @ResponseBody
    public String addFriendsById(AddFriendsEntity friendsEntity){
        logger.info(friendsEntity);
        contactService.addFriendsById(friendsEntity);
        return jsonSuccess("contact.add.friends.success");
    }

    /**
     * 查询聊天记录
     * @param search
     * @return
     */
    @RequestMapping("/queryChartReport")
    @ResponseBody
    public String queryChartReport(SearchChatEntity search){
        logger.info(search);
        List<ChatReportEntity> list = contactService.queryChartReport(search);
        return jsonDataSuccess("contact.query.chart.list.success",list);
    }

    /**
     * 添加聊天记录
     * @param chatReportEntity
     * @return
     */
    @RequestMapping("/addChartReport")
    @ResponseBody
    public String addChartReport(ChatReportEntity chatReportEntity){
        logger.info(chatReportEntity);
        contactService.addChartReport(chatReportEntity);
        return jsonSuccess("contact.add.chart.list.success");
    }

    /**
     * 测试testWebSooket
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/testWebSooket")
    @ResponseBody
    public String testWebSooket(HttpServletRequest request) throws IOException {
        String name = request.getParameter("name");
        logger.info(name);
        ChartWebSocketServer.sendAll(name);
        return jsonSuccess("web.socket.test");
    }


}
